package com.bitdf.txing.group.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bitdf.txing.group.constant.RedisConstant;
import com.bitdf.txing.group.dao.GroupCategoryDao;
import com.bitdf.txing.group.entity.GroupCategoryEntity;
import com.bitdf.txing.group.service.GroupCategoryService;
import com.bitdf.txing.group.vo.GroupCategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service("groupCategoryService")
public class GroupCategoryServiceImpl extends ServiceImpl<GroupCategoryDao, GroupCategoryEntity> implements GroupCategoryService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
//    @Override
//    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<GroupCategoryEntity> page = this.page(
//                new Query<GroupCategoryEntity>().getPage(params),
//                new QueryWrapper<GroupCategoryEntity>()
//        );
//
//        return new PageUtils(page);
//    }

    /**
     * 获取组团分类目录
     * @return
     */
    @Override
    public List<GroupCategoryVo> getCategory() {
        //先检查redis中是否有数据
        String s = stringRedisTemplate.opsForValue().get(RedisConstant.GROUP_CATEGORY);
        if(s!=null){
            List<GroupCategoryVo> list = JSON.parseObject(s, new TypeReference<List<GroupCategoryVo>>() {
            });
            return list;
        }

        //获取所有分类数据
        List<GroupCategoryEntity> list = this.list();
        //获取一级分类
        List<GroupCategoryEntity>  list1=this.getChildrenByList(list,0);
        //封装分类数据
        List<GroupCategoryVo> collect1 = list1.stream().map(item -> {
            GroupCategoryVo categoryVo = new GroupCategoryVo();
            BeanUtils.copyProperties(item, categoryVo);
            List<GroupCategoryEntity> list2 = this.getChildrenByList(list, item.getId());
            List<GroupCategoryVo> collect = list2.stream().map(entity -> {
                GroupCategoryVo categoryVo1 = new GroupCategoryVo();
                BeanUtils.copyProperties(entity, categoryVo1);
                return categoryVo1;
            }).collect(Collectors.toList());
            categoryVo.setChildrens(collect);
            return categoryVo;
        }).collect(Collectors.toList());
        //存入reids
        stringRedisTemplate.opsForValue().set(RedisConstant.GROUP_CATEGORY, JSON.toJSONString(collect1));
        return collect1;
    }

    /**
     * 根据父id获取所有的子分类
     * @param list
     * @param parentId
     * @return
     */
    private List<GroupCategoryEntity> getChildrenByList(List<GroupCategoryEntity> list, long parentId) {
        List<GroupCategoryEntity> collect = list.stream().filter((item) -> {
            return item.getParentId() == parentId;
        }).collect(Collectors.toList());
        return collect;
    }

}